﻿using System.Collections.Generic;

namespace PerfAssist.MemoryProfilerAdvanced.Editor.PAContrib
{
    public class MemObjHistory
    {
        private int _cursor = -1;

        private readonly List<ThingInMemory> _history = new List<ThingInMemory>();

        public void Clear()
        {
            _history.Clear();
            _cursor = -1;
        }

        public void OnObjSelected(ThingInMemory thing)
        {
            if (_cursor != -1)
            {
                if (!OutOfBounds() && _history[_cursor] == thing)
                    return;

                if (_cursor < _history.Count - 1) _history.RemoveRange(_cursor + 1, _history.Count - (_cursor + 1));
            }

            _history.Add(thing);

            if (_history.Count > 50) _history.RemoveRange(0, 10);

            _cursor = _history.Count - 1;
        }

        private bool OutOfBounds()
        {
            return _cursor < 0 || _cursor >= _history.Count;
        }

        public ThingInMemory TryGetPrev()
        {
            if (_history.Count == 0 || _cursor == 0 || _cursor == -1 || _cursor >= _history.Count)
                return null;

            if (OutOfBounds())
                return null;

            return _history[_cursor - 1];
        }

        public ThingInMemory MovePrev()
        {
            var thing = TryGetPrev();
            if (thing != null)
                _cursor--;
            return thing;
        }

        public ThingInMemory TryGetNext()
        {
            if (_history.Count == 0 || _cursor == _history.Count - 1 || _cursor == -1)
                return null;

            if (OutOfBounds())
                return null;

            return _history[_cursor + 1];
        }

        public ThingInMemory MoveNext()
        {
            var thing = TryGetNext();
            if (thing != null)
                _cursor++;
            return thing;
        }
    }
}